package main

import (
	"fmt"
	"zinx/ziface"
	"zinx/znet"
)

//ping test 自定义路由
type PingRouter struct {
	znet.BaseRouter //一定要先基础BaseRouter
}

//Test Handle
func (this *PingRouter) Handle(request ziface.IRequest) {
	fmt.Println("recv from client : msgId=", request.GetMsgId(), ", data=", string(request.GetData()))
	err := request.GetConnection().SendMsg(request.GetMsgId(), request.GetData())
	if err != nil {
		fmt.Println("call back ping ping ping error")
	}
}


//HelloZinxRouter Handle
type HelloZinxRouter struct {
    znet.BaseRouter
}

func (this *HelloZinxRouter) Handle(request ziface.IRequest) {
    fmt.Println("recv from client : msgId=", request.GetMsgId(), ", data=", string(request.GetData()))

    err := request.GetConnection().SendMsg(request.GetMsgId(), request.GetData())
    if err != nil {
        fmt.Println(err)
    }
}


func main() {
	//创建一个server句柄
	s := znet.NewServer("[zinx V0.3]")

	s.AddRouter(0, &PingRouter{})
	s.AddRouter(1, &HelloZinxRouter{})

	//2 开启服务
	s.Serve()
}
